# Setup fzf
# ---------

. $HOME/.config/fzf/key_bindings.zsh
. $HOME/.config/fzf/completion.zsh


# 自定义配置
export FZF_DEFAULT_COMMAND='fd -tf --strip-cwd-prefix --follow -HI -E .git -E /proc -E /sys'
# --bind ?:toggle-preview,alt-w:toggle-preview-wrap

#""-e
export FZF_DEFAULT_OPTS='
--height 60% --layout=reverse --border --info=inline --pointer '➤' --marker '┃'
--cycle
--bind alt-p:preview-up,alt-n:preview-down
--color=dark
--color=fg:-1,bg:-1,hl:#5fff87,fg+:cyan,bg+:99,hl+:#ffaf5f
--color=info:#af87ff,prompt:#5fff87,pointer:#ff87d7,marker:#ff87d7,spinner:#ff87d7
'


############# CTRL T 配置
#############
export FZF_CTRL_T_COMMAND='fd -tf --strip-cwd-prefix --follow -HI -E .git -E proc -E sys'
# export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
# export FZF_CTRL_T_OPTS="-1 -0"
export FZF_CTRL_T_OPTS="
  --preview 'bat -n --color=always {}' \
  --bind 'ctrl-/:change-preview-window(down|hidden|)' \
  --bind 'alt-p:change-preview-window(down|hidden|)'
"






############## 历史记录 CTRL R 配置
##############
# export FZF_CTRL_R_OPTS='--no-sort --exact'
# 长命令预览
export FZF_CTRL_R_OPTS="--preview 'echo {}' --preview-window down:3:hidden:wrap --bind '?:toggle-preview'"

# Eneter直接执行，不打印在终端
# fzf-history-widget-accept() {
  # fzf-history-widget
  # zle accept-line
# }
# zle     -N     fzf-history-widget-accept
# bindkey '^R' fzf-history-widget-accept

# 打印在终端
# bindkey '^X^R' fzf-history-widget


# 其它方案，CTRL E 编辑 enter 执行
fzf-history-widget() {
   local selected num
   setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null
   selected=( $(fc -rl 1 |  FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort --expect=ctrl-e $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
   local ret=$?
   if [ -n "$selected" ]; then

     local accept=0
     if [[ $selected[1] = ctrl-e ]]; then
       accept=1
       shift selected
     fi
     num=$selected[1]
     if [ -n "$num" ]; then
       zle vi-fetch-history -n $num
       [[ $accept = 0 ]] && zle accept-line
     fi
   fi
   zle reset-prompt
}
 


################## ALT C 配置
##################
export FZF_ALT_C_COMMAND="fd --type d --hidden"
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'"



alias fp="fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'"

# 按键绑定
export FZF_COMPLETION_TRIGGER=''
bindkey '^T' fzf-completion
bindkey '^I' $fzf_default_completion


. $HOME/.config/fzf/some_functions.zsh
